package com.sinodata.bsm.cicp.finder.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: 双节--> 节点 的发现程序， 发现节点名称
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:15:56          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HANodeFinder extends Finder {

    private static final Logger logger = Logger.getLogger(HANodeFinder.class);

    /**
     * 发现程序
     * 
     * @param pResValue  父资源的信息
     * @param pattrVals  父资源属性
     * @param attributes 资源自带的属性
     * @return 资源列表
     */
    /* public List<List<Serializable>> find(ResValue pResValue, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) {
         String shell = "clstat -o";
         BufferedReader reader = null;
         try {
             reader = CollectorUtil.getReader(shell, host);
             return this.parseReader(reader, pattrVals, attributes);
         } catch (IOException e) {
             logger.error("Failed to auto find HANode ", e);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e) {
                     logger.error("Failed to close IO resource ", e);
                 }
             }
         }
         return new ArrayList<List<Serializable>>();
     }*/

    /**
     * 解析命令响应结果
     * 
     * @param reader 命令响应结果
     * @param pattrVals  接口父资源 节点的属性信息
     * @param attributes 资源自带的属性
     * @return 资源列表
     * @throws IOException
     */
    /* private List<List<Serializable>> parseReader(BufferedReader reader, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) throws IOException {
         List<List<Serializable>> tlqResources = null;
         // 匹配: Node : xxx State : xxx
         Pattern codepatn = Pattern.compile("\\s*Node\\s*:\\s*(\\S*)\\s*\\S*\\s*:\\s*(\\S*)\\s*");

         String tmp = null;
         List<Serializable> resAndAttr = null;
         Matcher codeMach = null;
         String nodeName = null;
         tlqResources = new ArrayList<List<Serializable>>();
         while ((tmp = reader.readLine()) != null) {
             codeMach = codepatn.matcher(tmp);
             if (codeMach.matches()) {// 匹配的就是目标数据
                 resAndAttr = new ArrayList<Serializable>();
                 nodeName = codeMach.group(1);
                 resAndAttr.add(findRes(nodeName));
                 resAndAttr.addAll(findAttr(attributes, nodeName));
                 tlqResources.add(resAndAttr);
             }
         }
         return tlqResources;
     }*/

    /**
     * 创建ResValue
     * 
     * @param busCode 资源名称
     * @return ResValue
     */
    /*private ResValue findRes(String busCode) {
        ResValue resValue = new ResValue();
        resValue.setIp(host);
        resValue.setName(busCode);
        resValue.setInternalId(busCode);
        resValue.setDescript("Node");
        return resValue;
    }*/

    /**
     * 创建资源的属性
     * 
     * @param attributes
     * @param i  属性数组的角标
     * @param busCode 设置给属性的值
     * @return
     */
    /* private List<ResAttributeValValue> findAttr(ResAttributeValue[] attributes, String nodeName) {
         List<ResAttributeValValue> attValueArray = new ArrayList<ResAttributeValValue>();
         ResAttributeValValue nameAttr = new ResAttributeValValue();
         nameAttr.setResAttributeId(attributes[0].getId());// name
         nameAttr.setAttrValue(nodeName);
         attValueArray.add(nameAttr);
         return attValueArray;
     }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String shell = "clstat -o";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            return parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to auto find HANode ", e);
            return new ArrayList<String[]>();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
    }

    private List<String[]> parseReader(BufferedReader reader) throws IOException {
        List<String[]> list = new ArrayList<String[]>();
        Pattern codepatn = Pattern.compile("\\s*Node\\s*:\\s*(\\S*)\\s*\\S*\\s*:\\s*(\\S*)\\s*");
        String tmp = null;
        Matcher codeMach = null;
        String nodeName = null;
        while ((tmp = reader.readLine()) != null) {
            codeMach = codepatn.matcher(tmp);
            if (codeMach.matches()) {
                nodeName = codeMach.group(1);
                String[] info = new String[4];
                info[0] = nodeName;
                info[1] = nodeName;
                info[2] = "Node";
                info[3] = nodeName; //节点名称
            }
        }
        return list;
    }
}